﻿using Baidu.Aip.ImageSearch;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageQueryImageApp
{
    internal static class BDHelper
    {
        public static string UploadImage(string filepath)
        {
            // 设置APPID/AK/SK
            var APP_ID = "119655254";
            var API_KEY = "6AidFczAH2xLZj68U8KbKg4d";
            var SECRET_KEY = "SHxHwXVJzZXG547hVAEqE6FMlfEHphFD";

            var client = new ImageSearch(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超时时间

            try
            {
                var image = File.ReadAllBytes(filepath);
                var id = Guid.NewGuid().ToString();
                // 调用相似图检索—入库, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
                var options = new Dictionary<string, object>{
        {"brief", "{\"name\":\""+id+"\", \"id\":\""+id+"\"}"},
        {"tags", "100,11"}
    };
                var result = client.SimilarAdd(image, options);
                return result.ToString();
            }
            catch (Exception e)
            {
                return $"Error: {e.Message}";
            }
        }

        public static string QueryImage(string filepath) {
            // 设置APPID/AK/SK
            var APP_ID = "119655254";
            var API_KEY = "6AidFczAH2xLZj68U8KbKg4d";
            var SECRET_KEY = "SHxHwXVJzZXG547hVAEqE6FMlfEHphFD";

            var client = new ImageSearch(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超时时间

            try
            {
                var image = File.ReadAllBytes(filepath);
                // 调用相似图检索—查询, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.SimilarSearch(image);
                return result.ToString();
            }
            catch (Exception e)
            {
                return $"Error: {e.Message}";
            }
        }
    }
}
